package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.vioao.wechat.api.component.wxa.TemplateApi;
import com.github.vioao.wechat.bean.response.component.wxa.template.LibraryAddResponse;
import com.github.vioao.wechat.bean.response.component.wxa.template.LibraryListResponse;
import com.github.vioao.wechat.bean.response.component.wxa.template.TemplateListResponse;
import com.github.vioao.wechat.utils.Params;
import com.github.vioao.wechat.utils.client.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import one.stand.domain.NewLibraryAddResponse;
import one.stand.service.ComponentService;
import one.stand.service.TemplateService;
import org.apache.http.client.methods.HttpPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author: QIK
 * @CreateDate: 2019/5/13 18:46
 */
@Slf4j
@Service
@Transactional
public class TemplateServiceImpl extends PublicBaseService implements TemplateService {
    @Autowired
    @Lazy
    ComponentService componentService;

    @Override
    public LibraryListResponse libraryList(String appId, Integer offset, Integer count) {
        String tokne = componentService.getAuthorizerToken(appId);
        LibraryListResponse response = TemplateApi.libraryList(tokne, offset, count);
        log.info("模板列表：{}", JSON.toJSONString(response));
        return response;
    }

    @Override
    public LibraryAddResponse libraryAdd(String appId, String noticeId, Integer... code) {
        String token = componentService.getAuthorizerToken(appId);
        List<Integer> keywordIdList = new ArrayList<>();
        Collections.addAll(keywordIdList, code);
        LibraryAddResponse response = TemplateApi.libraryAdd(token, noticeId, JSON.toJSONString(keywordIdList));
        log.info("模板设置返回：{}", JSON.toJSONString(response));
        return response;
    }

    @Override
    public NewLibraryAddResponse libraryAdd4Subscribe(String appId, String noticeId, String sceneDesc, Integer... code) {
        String token = componentService.getAuthorizerToken(appId);
        log.info("getAuthorizerToken:{},{}",token,appId);
        List<Integer> keywordIdList = new ArrayList<>();
        Collections.addAll(keywordIdList, code);
        NewLibraryAddResponse response = addLibrary4Subscribe(token, noticeId, JSON.toJSONString(keywordIdList),sceneDesc);
        log.info("模板设置返回：{}", JSON.toJSONString(response));
        return response;
    }

    public static NewLibraryAddResponse addLibrary4Subscribe(String accessToken, String id, String keywordIdList, String sceneDesc) {
        String data = String.format("{\"tid\":\"%s\",\"kidList\":%s,\"sceneDesc\":\"%s\"}", id, keywordIdList, sceneDesc);
        Map<String, String> params = Params.create("access_token", accessToken).get();
        log.info("请求参数{}-{}", data, params.toString());
//        return HttpUtil.postJsonBean("https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate", params, data, NewLibraryAddResponse.class);
        String url = String.format("https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=%s",accessToken);
        return JSONObject.parseObject(one.stand.util.HttpUtil.sendPostJson(url, JSONObject.parseObject(data)),NewLibraryAddResponse.class);
    }

    @Override
    public TemplateListResponse list(String appId, Integer offset, Integer count) {
        String token = componentService.getAuthorizerToken(appId);
        TemplateListResponse response = TemplateApi.list(token, offset, count);
        return response;
    }
}
